package com.gh.pattern.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibHuangniu implements MethodInterceptor{

    public Object getInstance(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        //要把clazz设置为即将生成的新类的父类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        //业务增强
        System.out.println("我是cglib黄牛，我可以给你买票");
        methodProxy.invokeSuper(o,objects);
        System.out.println("我是cglib黄牛，如果买到票了我会通知你的");
        return null;
    }
}
